//-----------------------------------------------------------------------------
// File: TextBoxShape.cs
//
// Copyright (c) Dmitry Shuklin. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using ObjectiveDraw.Platform.Configuration;

namespace ObjectiveDraw.Platform.Drawing
{
	/// <summary>
	/// Summary description for TextBoxShape.
	/// </summary>
	public class TextBoxShape : SinglePointShapeBase
	{	

		public System.Text.StringBuilder m_Text = new System.Text.StringBuilder("Text");
		

		public Cerebrum.Direct3D.Forms.FlyweightTextBox TextBox;

		public TextBoxShape()
		{
		}

		//protected System.Drawing.Color m_TextForeColor;
		//[ObjectiveDraw.Platform.Serialization.SerializableProperty(KnownDrawComponent.TextForeColor)]
		//[ObjectiveDraw.Platform.Serialization.Formatter(typeof(ObjectiveDraw.Platform.Serialization.ColorFormater))]
		public System.Drawing.Color TextForeColor
		{
			get
			{
				return this.m_ForeColor;
			}
			/*set
			{
				this.m_TextForeColor = value;
			}*/
		}

		//protected string m_TextString;
		[ObjectiveDraw.Platform.Serialization.SerializableProperty(KnownDrawComponent.TextString)]
		public string TextString
		{
			get{return Convert.ToString(m_Text);}
			set
			{
				if (!m_Text.ToString().Equals(value))//				
				{
					m_Text.Remove(0, m_Text.Length);
					m_Text = m_Text.Append(value);					
				}				
			}
		}
	
		#region IDrawerShape Members

		protected override void OnSelectedChanged(EventArgs e)
		{
			base.OnSelectedChanged (e);
			if(this.TextBox!=null)
			this.TextBox.Focused = this.Selected;
		}

		
		public override float DistanceFrom(ObjectiveDraw.Platform.DrawerChartBase control, System.Drawing.PointF point)
		{
			float xo1 = OffsetFromAbscissa(control, PointAbscissa);
			float yo1 = OffsetFromOrdinate(control, PointOrdinate);

			double dx1 = point.X - xo1;
			double dy1 = point.Y - yo1;

			return (float)Math.Sqrt(dx1*dx1 + dy1*dy1);
		}

		public override void RenderShape(DrawerChartBase control, ChartInfoArgs cinfo, Cerebrum.Direct3D.Forms.RenderEventArgs re, float z)
		{
			//--if (cfg==null)
			//--	this.TextString = m_Text.ToString();
			//--Text = this.TextString;

			Microsoft.DirectX.Direct3D.Device device = re.Device;
			
			float x1 = OffsetFromAbscissa(control, PointAbscissa);

			float y1 = OffsetFromOrdinate(control, PointOrdinate);

			Cerebrum.Direct3D.Drawing.Font _Font = Cerebrum.Direct3D.Drawing.Graphics.DeviceObjects[device, this] as Cerebrum.Direct3D.Drawing.Font;

			if(_Font == null)
			{
				_Font = new Cerebrum.Direct3D.Drawing.Font(device, new System.Drawing.Font("Verdana", 12));
				Cerebrum.Direct3D.Drawing.Graphics.DeviceObjects[device, this] = _Font;
			}

			System.Drawing.PointF pnt = System.Drawing.PointF.Empty;
			pnt.X = (int)x1;
			pnt.Y = (int)y1;

			if(TextBox==null)
			{
				this.TextBox = new Cerebrum.Direct3D.Forms.FlyweightTextBox(pnt, m_Text/*, control*/);
				this.TextBox.Focused = this.Selected;
			}
			else
			{
				TextBox.Position = pnt;
				TextBox.Z = z;
			}
			//_Font.DrawText(m_Text, rect, z, Microsoft.DirectX.Direct3D.DrawTextFormat.Left, this.m_ForeColor);
			System.Drawing.Color clr0;
			object objColor = this.TextForeColor;
			if (objColor!= null)
				clr0 = (System.Drawing.Color)objColor;				
			else
				clr0 = this.m_ForeColor;

			TextBox.Color = clr0;//this.m_ForeColor;
			TextBox.Render(control, re, _Font);
		}

		public override void OnMouseUp(ObjectiveDraw.Platform.DrawerChartBase control, System.Windows.Forms.MouseEventArgs e, ref bool CaptureMouse)
		{
			base.OnMouseUp (control, e, ref CaptureMouse);
//			if(!m_Selected)
//			{
//				OnFormatObject(control, e);
//			}
		}

		public override void OnKeyDown(DrawerChartBase control, System.Windows.Forms.KeyEventArgs e)
		{
			base.OnKeyDown (control, e);
			if(this.TextBox!=null)
			{
				this.TextBox.KeyDown(control, e);
			}
		}

		public override void OnKeyPress(DrawerChartBase control, System.Windows.Forms.KeyPressEventArgs e)
		{
			base.OnKeyPress (control, e);
			if(this.TextBox!=null)
			{
				this.TextBox.KeyPress(control, e);
			}
		}

		#endregion

		private System.Windows.Forms.MenuItem m_MenuItem = null;
		private /*--ObjectiveDraw.Platform.IDrawerComponent--*/ object cfg = null;
		private void OnFormatObject(object sender, System.EventArgs e)
		{
			cfg = new object();
			Cerebrum.Desktop.Configuration.VariableElement root = new Cerebrum.Desktop.Configuration.VariableElement();
			root.Component = this;
			root.Elements.Add(Tools.BuildTextTree(root));					
			ObjectiveDraw.Platform.Workspace.ConfigureComponent(this, ObjectiveDraw.Platform.KnownDrawerCommands.ConfigurationUpdate, root);
			cfg = null;		
		}

		public override void ClearControlPoints()
		{
			base.ClearControlPoints();
			if(m_MenuItem != null)
			{
				ObjectiveDraw.Platform.Application _app = this.DrawerDocument.DrawerApplication;
				_app.ShapeMenu.MenuItems.Remove(m_MenuItem);
				m_MenuItem = null;
			}
		}

		public override bool UpdateShape(DrawerChartBase control, ChartInfoArgs cinfo, Cerebrum.Direct3D.Forms.DeviceEventArgs de, bool idle, out UpdateShapeInfo uinfo)
		{
			/*bool r = */base.UpdateShape (control, cinfo, de, idle, out uinfo);
			if(m_Selected)
			{
				if(ControlPoint!=null && m_MenuItem==null)
				{
					ObjectiveDraw.Platform.Application _app = this.DrawerDocument.DrawerApplication;
					if(_app.ShapeMenu!=null)
					{
						ControlPoint.ContextMenu = _app.ShapeMenu;
						m_MenuItem = ControlPoint.ContextMenu.MenuItems.Add("Format Object ...", new System.EventHandler(OnFormatObject));
					}
				}
			}
			return true;
		}


		protected override void OnDisposing(EventArgs e)
		{
			base.OnDisposing (e);
			Cerebrum.Direct3D.Drawing.Graphics.DeviceObjects.DisposeObject(this);
		}

		protected override bool LoadVariables(ObjectiveDraw.Platform.Serialization.FallbackChain storage, SerializationContext context)
		{
			bool baseValue = base.LoadVariables(storage, context);

			/*--object o = Workspace.LoadVariable(false, this, KnownDrawComponent.TextString);		
			if (o != null)
				Text = (string)o;

			m_Variables.Add(KnownDrawComponent.TextString,Text);

			o = Workspace.LoadVariable(false, this, KnownDrawComponent.TextForeColor);		
			if (o != null)
				m_Variables.Add(KnownDrawComponent.TextForeColor,(System.Drawing.Color)o);	
			else
				m_Variables.Add(KnownDrawComponent.TextForeColor, m_ForeColor);	--*/
			return baseValue;
		}

		protected override bool SaveVariables(ObjectiveDraw.Platform.Serialization.FallbackChain storage, SerializationContext context)
		{
			bool r = base.SaveVariables(storage, context);
			/*--if (cfg!=null)
				Text = (string)(m_Variables[KnownDrawComponent.TextString]);			
			Workspace.SaveVariable(this, KnownDrawComponent.TextString, Text);	--*/	
			return r;						
		}

			
		protected override bool InitVariables(bool loading)
		{			
			bool baseValue = base.InitVariables (loading);
			this.m_Text = new System.Text.StringBuilder(KnownDrawComponent.DefaultTextString);
			//this.TextString = m_Text.ToString();
			//this.TextForeColor = m_ForeColor;

			/*--if (m_Variables == null)
				m_Variables = new System.Collections.Hashtable ();
			Workspace.SaveVariable(this, KnownDrawComponent.TextForeColor, m_ForeColor);	
			m_Variables.Add( KnownDrawComponent.TextString,m_Text.ToString());
			Workspace.SaveVariable(this, KnownDrawComponent.TextString, Text);
			m_Variables.Add(KnownDrawComponent.TextForeColor,m_ForeColor);--*/
			return baseValue;
		}

		/*public string Text
		{
			get{return m_Text.ToString();}
			set
			{
				if (!m_Text.ToString().Equals(value))//				
				{
					m_Text.Remove(0, m_Text.Length);
					m_Text = m_Text.Append(value);					
				}				
			}
		}*/
	}
}
